﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Principal;

namespace eLibServer.Core.Services.Authentication
{
  public class eLibBaseIdentity : IIdentity
  {
    #region IIdentity Members

    public string AuthenticationType
    {
      get { return "eLibBaseAuthentication"; }
    }

    private bool _isAuthenticated = false;
    public bool IsAuthenticated
    {
      get { return _isAuthenticated; }
    }

    private string _username = String.Empty;
    public string Name
    {
      get { return _username; }
    }

    #endregion

    internal IDictionary<string, int> Roles = new Dictionary<string, int>();
    internal int UserId = 0;

    internal eLibBaseIdentity(string connectionString, string username, int userID, bool isAuthenticated)
		{
			_isAuthenticated = isAuthenticated;
			_username = username;
			UserId = userID;

			PopulateUserRoles(connectionString);
		}

    internal bool IsInRole(string role)
    {
      return Roles.ContainsKey(role.Trim().ToLowerInvariant());
    }
    
    internal void PopulateUserRoles(string connectionString)
    {
      Roles.Clear();
      
      //EntityCollection<SysRoleEntity> roles = UserFacade.GetRolesUserIsIn(connectionString, UserId);
      //foreach (SysRoleEntity role in roles)
      //{
      //  if (Roles.ContainsKey(role.Name.Trim().ToLowerInvariant()))
      //    continue;
      //  Roles.Add(role.NormalizedName, role.RoleId);
      //}
    }
  }
}
